/*
 * DS_test.c
 *
 *  Created on: 2023年6月30日
 *      Author: mcealt_DS
 */

#include "DS_test.h"

#include "bsp_usart.h"
#include "app_log.h"
#include "RBFTest.h"
#include <string.h>
#include "app_key_value_test.h"
#include "app_hub_api.h"
#include "em_cmu.h"

/*
 * 全局变量
 */
extern Buffer_t debug_RXBuff;

/****************************************************
 *                  test demo
 ****************************************************/
#ifdef HUB_DEVICE
const uint8_t str_device_type[] = "HUB";
#elif defined(RFM_DEVICE)
const uint8_t str_device_type[] = "RFM";
#else
#error "#ERROR:NO DEVICE TYPE DEFINE"
#endif
uint8_t buff[] = "12345678";
void DS_test_printf(void)
{
  APP_PRINTF("test DusunRBL This is %s device\r\n",str_device_type);
  APP_PRINTF_HEX("buff is:", buff,5);
  //test app_log_debug info error
  DS_app_log_debug("DS_app_log_debug\r\n");
  DS_app_log_info("DS_app_log_info\r\n");
  int a=100;
  DS_app_log_error("DS_app_log_error %d\r\n",a);
}


#include "bsp_nvm.h"
void DS_test_nvm(void)
{
    DS_nvmInit();
    char nvmData[] = "test nvm ok!\r\n";
    uint8_t readbuff[50];
    uint32_t key = 2;
    DS_nvmDelete( key );
    if(DS_nvmWrite( key,nvmData,strlen(nvmData)) != NVM_OK) return;
    if(DS_nvmRead( key,readbuff,strlen(nvmData))!= NVM_OK) return;
    APP_PRINTF("%s",readbuff);
}
void DS_test_hardfault(void)
{
    uint8_t *p = NULL;
    *p = 0XFF;
    while(1);
}

void DS_clk(void)
{
      uint32_t clock;
      clock = CMU_HFRCOEM23BandGet();
      APP_PRINTF("HFRCOEM23: %lu Hz\n", clock);
      clock = CMU_ClockFreqGet( cmuClock_CORE );
      APP_PRINTF("coreCLK = %ld\r\n", clock);
      clock = CMU_ClockFreqGet( cmuClock_SYSCLK );
      APP_PRINTF("SYSCLK = %ld\r\n", clock);
      clock = CMU_ClockFreqGet( cmuClock_SYSTICK );
      APP_PRINTF("SYSTICK = %ld\r\n", clock);
      while (1);
}

void DS_test(void)
{
    DS_clk();
  //DS_test_printf();        /*printf test demo*/
  //DS_test_nvm();
  //DS_test_hardfault();
  
} 
/****************************************************
 *                  test demo process
 ****************************************************/
void DS_test_RX_buff(void)
{
    uint8_t *data=NULL, cnt=0;
    uint8_t str[1024];

    while ( bufferIsEmpty(&debug_RXBuff) != true )
    {
        data = bufferRemove( &debug_RXBuff );
        str[cnt++] = data[0];
    }
    if( 0 != cnt )
    {
        for(uint8_t i = 0; i < cnt; i++)
        {
            APP_PRINTF("%c",str[i]);
        }
        cnt = 0;
    }
}
void DS_test_process(void)
{
  //DS_test_RX_buff();
  //RBFUpload_test();
  //RBFDownload_test();
  //RBFBroadcast_test();
  app_key_value_test();
}

//测试时，com口间隔50msRX255个字节的0x5a，
//com口把环形buf里收到的字节TX
//
void DS_test_Com_RX(void)
{
#ifdef BSP_ENABLE_COM
    uint8_t *data=NULL, cnt=0;
    uint8_t str[256];

    while ( bufferIsEmpty(&com_RXBuff) != true )
    {
        data = bufferRemove( &com_RXBuff );
        str[cnt++] = data[0];
    }
    if( 0 != cnt )
    {
        for(uint8_t i = 0; i < cnt; i++)
        {
            DS_com_Txbuff((char *)str,cnt);
            //printf("%c",str[i]);
            if(str[i] != 0x5a){
                printf("%c",str[i]);
            }
        }
        cnt = 0;
    }
#endif
}


void DS_test_hub_api(void)
{
  DS_hub_api_task();
}
